﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;

namespace 包覆门.HingeManager
{
    public partial class HingeList : Form
    {
        public HingeList()
        {
            InitializeComponent();
            this.dgHinge.AutoGenerateColumns = false;
        }
        public HingeDAL dal = new HingeDAL();
        //新增
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            HingeEditing he = new HingeEditing();
            he.IsNew = true;
            if(he.ShowDialog()==DialogResult.OK){
                LoadData();
            }

        }
        //编辑
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            int count = this.dgHinge.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选择要编辑的行");
                return;
            }
            HingeEditing ce = new HingeEditing();
            ce.IsNew = false;
            ce.EditingId = this.dgHinge.SelectedRows[0].Cells["id"].Value.ToString();
            if (ce.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }   
        }
        //删除
        private void tsbDel_Click(object sender, EventArgs e)
        {
            int count = this.dgHinge.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选择要删除的行");
                return;
            }
            string info = this.dgHinge.SelectedRows[0].Cells["hname"].Value.ToString();
            if (MessageBox.Show("确认要删除<" + info + ">么", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                int result = dal.DelById(this.dgHinge.SelectedRows[0].Cells["id"].Value.ToString());
                if (result == 1)
                {
                    MessageBox.Show("删除成功");
                    LoadData();
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
        }
        //关闭
        private void tsbClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        //查询
        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        //加载数据
        public void LoadData() {
            string hname = this.tbhinge.Text.Trim();
            this.dgHinge.DataSource = dal.HingeListAll(hname);
        }
        //加载
        private void HingeList_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void HingeList_FormClosing(object sender, FormClosingEventArgs e)
        {
            mainform.hinge = null;
        }
    }
}
